package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mouth-project/model/mysql"
	"mouth-project/pkg"
	"mouth-project/request"
	"net/http"
)

//商品发布

func GoodsCreate(c *gin.Context) {
	var goodsCreate request.GoodsCreateReq
	if err := c.ShouldBindJSON(&goodsCreate); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "服务器内部错误",
			"code": 500,
			"err":  err.Error(),
		})
		return
	}
	data := mysql.Goods{
		GoodsTitle:     goodsCreate.GoodsTitle,
		GoodsPrice:     goodsCreate.GoodsPrice,
		GoodsStock:     goodsCreate.GoodsStock,
		GoodsCover:     goodsCreate.GoodsCover,
		GoodsDetailsId: goodsCreate.GoodsDetailsId,
	}
	err := data.Create()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg":  "商品发布失败",
			"code": 400,
			"err":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "商品发布成功",
		"code": 200,
		"data": data,
	})
}

// 商品详情

func GoodsInfo(c *gin.Context) {
	var goodsInfo request.GoodsInfoReq1
	if err := c.ShouldBindJSON(&goodsInfo); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "服务器内部错误",
			"code": 500,
			"err":  err.Error(),
		})
		return
	}
	data := mysql.Goods{
		Model: gorm.Model{ID: uint(goodsInfo.Id)},
	}
	err := data.Info()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg":  "商品查询失败",
			"code": 400,
			"err":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "商品查询成功",
		"code": 200,
		"data": data,
	})
}

func Comment(c *gin.Context) {
	var commentReq request.Comment
	if err := c.ShouldBindJSON(&commentReq); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "服务器内部错误",
			"code": 500,
			"err":  err.Error(),
		})
		return
	}
	data := mysql.Comment{
		GoodsDetailsId: commentReq.GoodsDetailsId,
		UserId:         commentReq.UserId,
		Text:           commentReq.Text,
		Pid:            commentReq.Pid,
	}
	err := data.Create()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg":  "评论失败",
			"code": 400,
			"err":  err.Error(),
		})
		return
	}
	//调用敏感词审核
	if !pkg.Baidu(commentReq.Text) {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg":  "发言违规,请文明用语",
			"code": 400,
			"err":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "评论成功",
		"code": 200,
		"data": data.Text,
	})
}
